package com.syq.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syq.pojo.Profile;
import com.syq.service.ProfileService;
import com.syq.service.impl.ProfileServiceImpl;
import com.syq.utils.StringUtils;


import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;


@WebServlet(name = "ProfileServlet", value = "/profileservlet")
@MultipartConfig(maxFileSize = 1024*1024*5,maxRequestSize = 1024*1024*20)
public class ProfileServlet extends BaseServlet {
   public String list(HttpServletRequest request,HttpServletResponse response)throws Exception{
      String pageNum = request.getParameter("pageNum");
      String pageSize = request.getParameter("pageSize");

      int pn=1;
      int ps=3;
      try {
         if(!StringUtils.isEmpty(pageNum)){
            pn= Integer.parseInt(pageNum);
            if(pn<1){
               pn=1;
            }
         }
         if(!StringUtils.isEmpty(pageSize)){
            ps=Integer.parseInt(pageSize);
            if(ps<1){
               ps=3;
            }
         }
      } catch (NumberFormatException e) {
         e.printStackTrace();
      }
      ProfileService profileService=new ProfileServiceImpl();
      PageHelper.startPage(pn,ps);
      List<Profile> lis = profileService.findAll();
      PageInfo<Profile> pageInfo=new PageInfo<>(lis);
      request.setAttribute("pageinfo",pageInfo);
      return "list.jsp";
   }

   public String detail(HttpServletRequest request,HttpServletResponse response)throws Exception{
      String id = request.getParameter("id");
      if (StringUtils.isEmpty(id)){
         request.setAttribute("msg","查询失败");
         return "/profileservlet?method=list";
      }
      ProfileService profileService=new ProfileServiceImpl();
      try {
         Profile profile=profileService.findById(Integer.parseInt(id));
         System.out.println(profile);
         request.setAttribute("profiles",profile);
         return "/detail.jsp";
      } catch (Exception e) {
         e.printStackTrace();
         return "redirect:/profileservlet?method=list";
      }
   }

   public String findById(HttpServletRequest request,HttpServletResponse response)throws Exception{
      String id = request.getParameter("id");
      if (StringUtils.isEmpty(id)){
         request.setAttribute("msg","失败");
         return "/profileservlet?method=list";
      }
      ProfileService profileService=new ProfileServiceImpl();
      try {
         Profile profile=profileService.findById(Integer.parseInt(id));
         System.out.println(profile);
         request.setAttribute("profiles",profile);
         return "/update.jsp";
      } catch (Exception e) {
         e.printStackTrace();
         return "redirect:/profileservlet?method=list";
      }
   }
   public String update(HttpServletRequest request,HttpServletResponse response)throws Exception{
      String id = request.getParameter("id");
      String name = request.getParameter("name");
      String birthday = request.getParameter("birthday");
      String gender = request.getParameter("gender");
      String career = request.getParameter("career");
      String address = request.getParameter("address");
      String mobile = request.getParameter("mobile");
      Part picture = request.getPart("picture");


      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Profile profile=new Profile(Integer.parseInt(id),name,sdf.parse(birthday),gender,career,address,mobile,null);
      request.setAttribute("profiles",profile);

      if (StringUtils.isEmpty(id)){
         request.setAttribute("msg","id不能为空");
         return "/update.jsp";
      }
      if (StringUtils.isEmpty(name)){
         request.setAttribute("msg","名字不能为空");
         return "/update.jsp";
      }
      if (StringUtils.isEmpty(birthday)){
         request.setAttribute("msg","生日不能为空");
         return "/update.jsp";
      }
      if (StringUtils.isEmpty(name)){
         request.setAttribute("msg","性别不能为空");
         return "/update.jsp";
      }
      if (StringUtils.isEmpty(career)){
         request.setAttribute("msg","职业不能为空");
         return "/update.jsp";
      }
      if (StringUtils.isEmpty(address)){
         request.setAttribute("msg","地址不能为空");
         return "/update.jsp";
      }
      if (StringUtils.isEmpty(mobile)){
         request.setAttribute("msg","手机不能为空");
         return "/update.jsp";
      }

      String basepath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images");
      File dir=new File(basepath);
      if (!dir.exists()){
         dir.mkdirs();
      }

      String submittedFileName = picture.getSubmittedFileName();
      String filename=null;
      if (submittedFileName!=null) {
         filename = UUID.randomUUID().toString().replace("-", "").substring(16);
         String ext = submittedFileName.substring(submittedFileName.indexOf("."));
         filename = filename + ext;
         picture.write(basepath + File.separator + filename);
      }
      System.out.println(filename);

      ProfileService profileService=new ProfileServiceImpl();
      profile=new Profile(Integer.parseInt(id),name,sdf.parse(birthday),gender,career,address,mobile,filename);
      try {
         profileService.update(profile);
         request.setAttribute("msg","修改成功");
         return "/profileservlet?method=list";
      } catch (Exception e) {
         e.printStackTrace();
         request.setAttribute("msg","修改失败");
         return "/update.jsp";
      }
   }

   public String add(HttpServletRequest request,HttpServletResponse response)throws Exception{
      String name = request.getParameter("name");
      String birthday = request.getParameter("birthday");
      String gender = request.getParameter("gender");
      String career = request.getParameter("career");
      String address = request.getParameter("address");
      String mobile = request.getParameter("mobile");
      Part picture = request.getPart("picture");



      if (StringUtils.isEmpty(name)){
         request.setAttribute("msg","名字不能为空");
         return "/add.jsp";
      }
      if (StringUtils.isEmpty(birthday)){
         request.setAttribute("msg","生日不能为空");
         return "/add.jsp";
      }
      if (StringUtils.isEmpty(name)){
         request.setAttribute("msg","性别不能为空");
         return "/add.jsp";
      }
      if (StringUtils.isEmpty(career)){
         request.setAttribute("msg","职业不能为空");
         return "/add.jsp";
      }
      if (StringUtils.isEmpty(address)){
         request.setAttribute("msg","地址不能为空");
         return "/add.jsp";
      }
      if (StringUtils.isEmpty(mobile)){
         request.setAttribute("msg","手机不能为空");
         return "/add.jsp";
      }



      String basepath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images");
      File dir=new File(basepath);
      if (!dir.exists()){
         dir.mkdirs();
      }

      String submittedFileName = picture.getSubmittedFileName();
      String filename= UUID.randomUUID().toString().replace("-","").substring(16);
      String ext = submittedFileName.substring(submittedFileName.indexOf("."));
      filename=filename+ext;
      picture.write(basepath+File.separator+filename);


      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      ProfileService profileService=new ProfileServiceImpl();
      Profile profile=new Profile(0,name,sdf.parse(birthday),gender,career,address,mobile,filename);
      try {
         profileService.add(profile);
         request.setAttribute("msg","添加成功");
         return "/profileservlet?method=list";
      } catch (Exception e) {
         e.printStackTrace();
         request.setAttribute("msg","添加失败");
         return "/add.jsp";
      }
   }
}
